/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.fraction;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;

import com.yc.video.config.ConstantKeys;
import com.yc.video.config.VideoInfoBean;
import com.yc.video.player.VideoViewManager;
import com.yc.video.tool.PlayerUtils;

import org.yczbj.ycvideoplayer.ability.DetailAbility;
import org.yczbj.ycvideoplayer.ability.IntentKeys;
import org.yczbj.ycvideoplayer.adapter.VideoRecyclerViewAdapter;

/**
 * Recyclerview Video
 *
 * @since 2021-05-10
 */
public class SeamlessPlayFragment extends RecyclerViewAutoPlayFragment {
    private boolean mSkipToDetail;

    /**
     * 构造函数
     */
    public SeamlessPlayFragment() {
    }

    @Override
    protected void initComponent(Component rootComponent, Intent intent) {
        super.initComponent(rootComponent, intent);

        // 提前添加到VideoViewManager，供详情使用
        VideoViewManager.instance().add(mVideoView, "seamless");

        mAdapter.setOnItemClickListener(position -> {
            mSkipToDetail = true;
            Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(DetailAbility.class)
                .build();
            Intent newIntent = new Intent();
            newIntent.setOperation(operation);
            VideoInfoBean videoBean = mVideos.get(position);
            if (mCurPos == position) {
                // 需要无缝播放
                newIntent.setParam(IntentKeys.SEAMLESS_PLAY, true);
                newIntent.setParam(IntentKeys.TITLE, videoBean.getTitle());
            } else {
                // 无需无缝播放，把相应数据传到详情页
                mCurPos = position;

                // 需要把控制器还原
                mController.setPlayState(ConstantKeys.CurrentState.STATE_IDLE);
                newIntent.setParam(IntentKeys.SEAMLESS_PLAY, false);
                newIntent.setParam(IntentKeys.URL, videoBean.getVideoUrl());
                newIntent.setParam(IntentKeys.TITLE, videoBean.getTitle());
                mCurPos = position;
            }
            startAbility(newIntent, 0);
        });
    }

    @Override
    protected void startPlay(int position) {
        mVideoView.setController(mController);
        super.startPlay(position);
    }

    @Override
    protected void pause() {
        if (!mSkipToDetail) {
            super.pause();
        }
    }

    @Override
    protected void resume() {
        if (mSkipToDetail) {
            mSkipToDetail = false;
        } else {
            super.resume();
        }
    }

    private void restoreVideoView() {
        // 还原播放器
        Component itemView = mListContainer.getComponentAt(mCurPos);
        VideoRecyclerViewAdapter.Holder viewHolder = (VideoRecyclerViewAdapter.Holder) itemView.getTag();
        mVideoView = VideoViewManager.instance().get("seamless");
        PlayerUtils.removeViewFormParent(mVideoView);
        viewHolder.mPlayerContainer.addComponent(mVideoView, 0);

        mController.addControlComponent(viewHolder.mPrepareView, true);
        mController.setPlayState(mVideoView.getCurrentPlayState());
        mController.setPlayerState(mVideoView.getCurrentPlayerState());
        mVideoView.setController(mController);
    }
}
